package com.cskt.petproject178.service.impl;

import com.cskt.petproject178.mapper.*;
import com.cskt.petproject178.pojo.param.SearchProductParam;
import com.cskt.petproject178.pojo.vo.*;
import com.cskt.petproject178.service.ProductParamService;
import com.cskt.petproject178.service.third.AliyunOSSService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 27013
 **/

@Service
public class ProductParamServiceImpl implements ProductParamService {
    @Resource
    private SpuMapper spuMapper;
    @Resource
    private ProductCategoryMapper productCategoryMapper;
    @Resource
    private ProductDetailPicMapper productDetailPicMapper;
    @Resource
    private SkuMapper skuMapper;

    @Resource
    private AliyunOSSService aliyunOSSService;

    @Override
    public PageVO<SearchProductVO> getSearchProductVOByParam(SearchProductParam param) {
        List<SearchProductVO> productVOS=spuMapper.getSearchProductVOByParam(param);
        Integer total=spuMapper.getSearchProductVOByParamCount(param);
        //开始处理图片路径
        productVOS.forEach(searchProductVO -> searchProductVO.setDefaultPicUrl(aliyunOSSService.getObjUrl(searchProductVO.getDefaultPicUrl())));
        return PageVO.page(param.getPageNo(), param.getPageSize(), total, productVOS);
    }

    @Override
    public SpusVO getSupsvoById(Long supId) {

        SpuVO spu = spuMapper.getSpuVOById(supId);
        List<ProductDetailPicVO> productDetailPicVOBySpuId = productDetailPicMapper.getProductDetailPicVOBySpuId(supId);
        List<CategoriesVO> categories = productCategoryMapper.getCategoriesList();
        //开始处理图片路径
       productDetailPicVOBySpuId.forEach(productDetailPicVO -> productDetailPicVO.setPicUrl(aliyunOSSService.getObjUrl(productDetailPicVO.getPicUrl())));
        //开始处理图片路径
        List<SkuVO> skuVOBySpuId = skuMapper.getSkuVOBySpuId(supId);
        skuVOBySpuId.forEach(skuVO -> skuVO.getSkuPicList().forEach(skuPicVO -> skuPicVO.setPicUrl(aliyunOSSService.getObjUrl(skuPicVO.getPicUrl()))));
        spu.setProductDetailPicList(productDetailPicVOBySpuId);
        return new SpusVO(spu,categories,skuVOBySpuId);
    }
}
